package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;	

import com.mysql.jdbc.Statement;
	
public class ValidarReserva  extends HttpServlet{


		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
			
			
		//	String user = req.getParameter("user");
			String idpqt = req.getParameter("paquetelist");//paquete.
			String idvuelo = req.getParameter("vueloasignado");//que vuelo es
			int nitamos = Integer.parseInt(req.getParameter("cantClientes"));//asientos q nitamos
			//String[] clientes = req.getParameterValues("clientesasignados");
			String[] clientes =req.getParameter("idclisconcat").split("|");
			String clientesConcat =req.getParameter("idclisconcat");
			java.sql.Connection con;
			
			
			
			ResultSet rs;
			ResultSet rs2;
			//boolean pasa = true;//flag.
			
			
			String databaseName = "grupo_1_db";
		  try {
			Class.forName("com.mysql.jdbc.Driver"); 
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + databaseName,"root","ignacio"); 
			Statement st1= (Statement) con.createStatement(); 		
			Statement st2= (Statement) con.createStatement(); 	
			rs=st1.executeQuery("SELECT asientosDisponibles,descripcion FROM vuelos where idvuelo="+idvuelo);	
			rs2=st2.executeQuery("SELECT descripcion FROM paquetes_viajes where id="+idpqt);	
		
			String nombrepqt = "Paquete Basico";
			String strcantcli = req.getParameter("cantClientes");//cantidad de clientes en este pqt
			if(rs2.next()){nombrepqt = rs2.getString(1);}
			 while(rs.next()) 
				{ 	
					int asientosDisponibles = Integer.parseInt(rs.getString(1));
					String nombrevuelo = rs.getString(2);
					
					
					if(asientosDisponibles>=nitamos){
						String btnvolver =  "<input type='button' onclick='location.href=\"Index.jsp\"' value='Volver' />";
						if(nitamos>0){
					//	response(req,resp, "Hay lugares disponibles para hacer la reserva, necesitamos " + nitamos + " y hay " + 
					//asientosDisponibles + "+BotonConfirmarReserva");	
						
						String msg = "Hay lugares disponibles para hacer la reserva, necesitamos " + nitamos + " asientos y hay " + asientosDisponibles + " asientos";
						   req.setAttribute("message", msg); // This will be available as ${message}
						   req.setAttribute("formaction", "GuardarReserva"); // This will be available as ${message}
						   
						   
						   req.setAttribute("mostrar", "inline"); // mostrar
						   
					
						   
						   String valuesPasar = "<input type='hidden' id='idvuelo' name='idvuelo' value='"+idvuelo+"'>" +
								   "<input type='hidden' id='idpqt' name='idpqt' value='"+idpqt+"'>" +
								   "<input type='hidden' id='cantcl' name='cantcl' value='"+strcantcli+"'>" + 
						   "<input type='hidden' id='clientesC' name='clientesC' value='"+clientesConcat+"'>";
						   
						    
						  
						   
						  /* if (clientes .length > 0) {
						             for (int i=0;i<clientes.length;i++) {
						            	 valuesPasar = valuesPasar +(clientes[i]);
						                            }
						        }*/
						 
						   req.setAttribute("invi_var", valuesPasar); // 
						   req.setAttribute("nombrevuelo", nombrevuelo); // 
						   req.setAttribute("nombrepqt", nombrepqt); // 
						   req.setAttribute("cantClientes", strcantcli); //
						   
						   req.setAttribute("btnfinal",  "<input type='submit' name='envairreserva' id='envairreserva' value='Guardar Reserva' />  " + btnvolver);
					        req.getRequestDispatcher("/ValidarReserva.jsp").forward(req, resp);
					        
						} else {
							//hay disponibles pero no cargamos clientes!
							
							String msg = "No se cargo ningun cliente!";
							req.setAttribute("message", msg); // This will be available as ${message}
							   req.setAttribute("formaction", ""); // This will be available as ${message}
							   req.setAttribute("mostrar", "none"); // ocultar							   
							   req.setAttribute("btnfinal", btnvolver);
						        req.getRequestDispatcher("/ValidarReserva.jsp").forward(req, resp);
							
						}
					        
						
						
						
										
					} else {
					
						String msg = "<b>No hay lugares disponibles para hacer la reserva";
						
						 req.setAttribute("message", msg); // This will be available as ${message}
						   req.setAttribute("formaction", ""); // This will be available as ${message}
						   req.setAttribute("mostrar", "none"); // ocultar
						   
						   req.setAttribute("btnfinal",  "<input type='button' onclick='location.href=\"Index.jsp\"' value='Volver' />");
					        req.getRequestDispatcher("/ValidarReserva.jsp").forward(req, resp);
						
						
						
					}
				} 
			 
			 
			 
			 
			 
			  if(con!=null) {con.close();}
			
		  	}
			catch (Exception e)
			{
				response(req,resp, "Error en la conexion con la base de datos");
			}
		  
		 
	
		  
			
			//vueloasignado id
		
			//response(resp, msjdevolver);
			
			//if ("edu4java".equals(user) && "eli4java".equals(pass)) {
			//	response(resp, "login ok");
			//} else {
			//	response(resp, "invalid login");
			//}
		}

		private void response(HttpServletRequest req,HttpServletResponse resp, String msg)
				throws IOException, ServletException {
			PrintWriter out = resp.getWriter();
			out.println("<html>");
			out.println("<body>");
			
			//out.println("<script>alert(1);</script>");
			out.println("<t1>" + msg + "</t1>");
			out.println("</body>");
			out.println("</html>");
			
			  // String message = "Hello World";
			 //  req.setAttribute("message", msg); // This will be available as ${message}
		      //  req.getRequestDispatcher("/ValidarReserva.jsp").forward(req, resp);
		 

			
		}
		

}
